<?php

/**
 * Виджет FOOTER
 * 
 * @author Kosty Ryabichenko <k.ryabichenko@gmail.com>
 * @version 1.0
 */
class FooterWidget extends CWidget {

    public function init() {
        
        $model = new Feedback();
        $data = Yii::app()->request->getParam('Feedback', null);
        if(!empty($data)){
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->mailer->send('info@flatrent.in.ua', 'Обратная связь', 
                '<p>
                    <b>Имя: </b>' . $model->name . '<br />
                    <b>Email: </b>' . $model->email . '<br />
                    <b>Сообщение: </b>' . $model->message . '<br />
                </p>'
                );
            }
        }
        
        
        $this->render('footer', array(
            'flats' => $this->getRecommendFlat(3),
            'model' => $model,
        ));
    }

    public function run() {
        // этот метод будет вызван внутри CBaseController::endWidget()
    }

    private function getRecommendFlat($limit) {

        $search = Yii::app()->session->get("searchParams", null);

        $criteria = new CDbCriteria;

        if (!empty($search)) {

            if (!empty($search['type_id'])) {
                $criteria->addCondition("type_id = " . $search['type_id'] . "");
            }

            if (!empty($search['guests'])) {
                $criteria->addCondition("guests = " . $search['guests'] . "");
            }

            $from = !empty($search['inputPriceFrom']) ? $search['inputPriceFrom'] : Flat::getPriceFromCity($search['city_id'], 'min');
            $to = !empty($search['inputPriceTo']) ? $search['inputPriceTo'] : Flat::getPriceFromCity($search['city_id'], 'max');
            $criteria->addBetweenCondition("price", $from, $to);
        }

        $criteria->addCondition("is_active = 1");

        $criteria->limit = $limit;
        $criteria->offset = '0';

        $flats = Flat::model()->findAll($criteria);

        return !empty($flats) ? $flats : array();
    }

}

?>
